import { FMVCS } from "../../fgui/fmvc/FMVCS";
import ResultCtrl from "./ResultCtrl";
import ResultData from "./ResultData";
import EventSystem from "../../core/EventSystem";
import { GlobalEvent } from "../global/Global";
import PlayerCtrl from "../player/PlayerCtrl";
import { ConstDefine } from "../common/ConstDefine";
import GameMainCtrl from "../gamemain/GameMainCtrl";

export default class ResultView extends FMVCS.FView {
    private _data: ResultData = null;
    private _isWin: boolean = false;
    onConstruct(){
        super.onConstruct();
    }

    clickHandlers(){
        return [
            [this._views[`btnMenu`], this.onBtnBackToMenu, this],
            [this._views[`btnRestart`], this.onBtnRestart, this],
            [this._views[`btnNext`], this.onBtnNext, this],
            [this._views[`btnShare`], this.onBtnShare, this],
        ];
    }

    onOpenCallBack(){
        this._data = ResultCtrl.data;
        this._isWin = this._data.vo.isWin;
        if(this._isWin)
            this.playAnim();

        this.onFlush();
    }

    playAnim(){
        let eff = this.getTransition(`anim`);
        eff.stop();
        eff.play();
    }

    onBtnBackToMenu(){
        if(GameMainCtrl.data.getIsNormal()){
            EventSystem.emit(GlobalEvent.UIListenID.UI_LevelChooseView_Open);
        }
        else{
            EventSystem.emit(GlobalEvent.UIListenID.UI_MapLocalView_Open);
        }
        EventSystem.emit(GlobalEvent.UIListenID.UI_GameMainView_Close);
        this.onClose();
    }

    onBtnRestart(){
        EventSystem.emit(GlobalEvent.UIListenID.UI_BtnRestart_Click);
    }

    onBtnNext(){
        if(GameMainCtrl.data.getIsNormal()){
            if(PlayerCtrl.data.vo.maxLevel > this._data.vo.Level){
                if(PlayerCtrl.data.vo.maxLevel < Object.keys(ResultCtrl.data.config).length){
                    EventSystem.emit(GlobalEvent.UIListenID.UI_LevelChoose, this._data.vo.Level + 1);
                    this.onClose();
                }else{
                    EventSystem.emit(GlobalEvent.UIListenID.UI_ShowTips, `你已通关所有关卡!`);
                }
            }else{
                EventSystem.emit(GlobalEvent.UIListenID.UI_ShowTips, `通关当前关卡后解锁!`);
            }
        }else{
            EventSystem.emit(GlobalEvent.UIListenID.UI_CustomMapNext);
        }
        this.onClose();
    }

    onBtnShare(){

    }

    onFlush(type: string = `all`){
        switch(type){
            case `all`: {
                this._updateLblText();
                this._updateLblShare();
                this._updateLblLevel();
                this._updateImgLevel();
                this._updateLblMapName();
                break;
            }
        }
    }

    private _updateLblText(){
        let lblText = this._views[`lblText`].asLabel;
        lblText.text = this._isWin ? `恭喜过关!` : `失败!`;
    }

    private _updateLblShare(){
        let lbl = this._views[`btnShare`].getChild(`lblText`).asLabel;
        lbl.text = this._isWin ? `还有谁!` : `好友求助`;
    }

    private _updateLblLevel(){
        let lbl = this._views[`lblLevel`].asLabel;
        if(this._data.vo.pattern == ConstDefine.GameMain.Pattern.normal)
            lbl.text = `关卡` + (this._data.vo.Level + 1);
        else
            lbl.text = `自定义`;
    }

    private _updateImgLevel(){
        let img = this._views[`imgContent`].asLoader;
        img.icon = fgui.UIPackage.getItemURL("Common", `${this._data.vo.Level}`);
        img.visible = (this._data.vo.pattern == ConstDefine.GameMain.Pattern.normal);
    }

    private _updateLblMapName(){
        let lbl = this._views[`lblMapName`].asLabel;
        lbl.text = `房间:\n` + this._data.curMap.mapName;
        lbl.visible = !(this._data.vo.pattern == ConstDefine.GameMain.Pattern.normal);
    }
}